package com.wang.gallerybackend.manager.upload;

import cn.hutool.core.io.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 文件上传实现
 */
@Service
public class FilePictureUploadImpl extends PictureUpload {
	@Override
	protected void processFile(Object inputSource, File file) throws IOException {
		MultipartFile multipartFile = (MultipartFile) inputSource;
		multipartFile.transferTo(file);
	}

	@Override
	protected String getOriginalFilename(Object inputSource) {
		MultipartFile multipartFile = (MultipartFile) inputSource;
		return multipartFile.getOriginalFilename();
	}

	@Override
	protected void validPicture(Object inputSource) {
		MultipartFile multipartFile = (MultipartFile) inputSource;
		if (multipartFile == null) {
			throw new RuntimeException("文件不能为空");
		}
		//1.校验文件大小
		long size = multipartFile.getSize();
		if (size > MAXSIZE) {
			throw new RuntimeException("文件大小不能超过2M");
		}
		//2.校验文件类型
		String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
		if (!ALLOW_FILE_SUFFIX.contains(fileSuffix)) {
			throw new RuntimeException("文件类型错误");
		}

	}
}
